/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-10-10 16:25:31
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-10-23 20:23:24
 * @FilePath: \实训二——宏业找房\egg\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const { Controller } = require('egg');
class AuditController extends Controller {
    // 获取经纪人数据
    async getAudit() {
        const { ctx } = this;
        const results = await this.app.mysql.select('audit');
        const { one, two, three, four, status, pageCode, pageSize } = ctx.request.body;
        let newLists = results;
        if (status) {
            newLists = newLists.filter(v => v.status === status)
        }
        if (one) {
            newLists = newLists.filter(v => v.title.includes(one))
        }
        if (two) {
            newLists = newLists.filter(v => v.title.includes(two))
        }
        if (three) {
            newLists = newLists.filter(v => v.title.includes(three))
        }
        if (four) {
            newLists = newLists.filter(v => v.name.includes(four))
        }

        let total = newLists.length;
        newLists = newLists.slice((pageCode * 1 - 1) * pageSize, pageCode * pageSize)
        if (results) {
            ctx.body = {
                code: 200,
                list: newLists,
                total,
                msg: '获取成功',
                results
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '获取失败'
            }
        }
    }
    // 删除数据
    async delAudit() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('audit', { id });
        if (result) {
            const results = await this.app.mysql.select('audit');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list: results
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '删除失败'
            }
        }

    }

    // 编辑
    async editAudit() {
        const { ctx } = this;
        const { name, title, sum, sort, area, status, pageCode, pageSize, id } = ctx.request.body;
        const row = {
            name, title, sum, sort, area, status
        };

        const options = {
            where: {
                id
            }
        };
        const result = await this.app.mysql.update('audit', row, options);
        if (result) {
            ctx.body = {
                code: 200,
                msg: '修改成功',
                list: result
            }
        } else {
            ctx.body = {
                code: 500,
                msg: '修改失败',
            }
        }
    }

}

module.exports = AuditController;
